package com.dfiov.cloud.msmtools.mapper.entity.msbluetooth;

import com.dfiov.cloud.msmtools.utils.JsonUtil;
import com.dfiov.cloud.msmtools.utils.RSAUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.security.spec.InvalidKeySpecException;
import java.time.LocalDateTime;

@ApiModel("蓝牙钥匙敏感信息")
@Data
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public class BluetoothSensitiveInfo {
	@ApiModelProperty("tbox蓝牙名称")
	private String bluetoothConName;

	@ApiModelProperty("tbox蓝牙连接key")
	private String bluetoothConKey;

	@ApiModelProperty("tbox蓝牙访问key")
	private String bluetoothAccessKey;

	@ApiModelProperty("到期时间")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	@JsonDeserialize(using = LocalDateTimeDeserializer.class)
	@JsonSerialize(using = LocalDateTimeSerializer.class)
	private LocalDateTime keyExpireTime;

	public BluetoothSensitiveInfo() {

	}
    @JsonIgnore
	public String getEncryptInfo(String pubKey) {
		log.debug("getEncryptInfo  pubkey = {}", pubKey);
		if (StringUtils.isNoneBlank(pubKey)) {
			String json = JsonUtil.objToJsonStr(this);
			try {
				return RSAUtils.encryptByPublic(json.getBytes(), pubKey);
			} catch (InvalidKeySpecException | IOException e) {
				log.error("rsa encrypt fail!", e);
			}
		}
		return "rsa encrypt fail!";
	}
}
